package com.ssy.lingxi.member.merchant.serviceimpl.base;

import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.member.merchant.entity.BasePlatformProcessDO;
import com.ssy.lingxi.member.merchant.entity.PlatformProcessRuleDO;
import com.ssy.lingxi.member.merchant.entity.RoleDO;
import com.ssy.lingxi.member.merchant.model.bo.ProcessBO;
import com.ssy.lingxi.member.merchant.repository.BasePlatformProcessRepository;
import com.ssy.lingxi.member.merchant.repository.PlatformProcessRuleRepository;
import com.ssy.lingxi.member.merchant.service.base.IBasePlatformProcessService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 角色关联的平台会员审核（会员注册）流程相关接口实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2021-11-10
 */
@Service
public class BasePlatformProcessServiceImpl implements IBasePlatformProcessService {
    @Resource
    private PlatformProcessRuleRepository platformProcessRuleRepository;

    @Resource
    private BasePlatformProcessRepository basePlatformProcessRepository;

    /**
     * 查询角色关联的平台会员审核（会员注册）流程
     *
     * @param role 会员角色
     * @return 流程的processKey
     */
    @Override
    public Wrapper<ProcessBO> findRolePlatformProcess(RoleDO role) {
        return findRolePlatformProcess(role.getId());
    }

    /**
     * 查询角色关联的平台会员审核（会员注册）流程
     *
     * @param roleId 会员角色Id
     * @return 流程的processKey
     */
    @Override
    public Wrapper<ProcessBO> findRolePlatformProcess(Long roleId) {
        PlatformProcessRuleDO platformProcessRule = platformProcessRuleRepository.findFirstByRoleId(roleId);
        if(platformProcessRule == null) {
            BasePlatformProcessDO basePlatformProcess = basePlatformProcessRepository.findFirstByDefaultProcess(true);
            if(basePlatformProcess == null) {
                return Wrapper.fail(ResponseCode.MC_MS_ROLE_PLATFORM_PROCESS_DOES_NOT_EXIST);
            }

            return Wrapper.success(new ProcessBO(basePlatformProcess.getProcessKey(), basePlatformProcess.getEmptyProcess()));
        }

        return Wrapper.success(new ProcessBO(platformProcessRule.getProcessKey(), platformProcessRule.getEmptyProcess()));
    }
}
